import { PluginCommand } from "./PluginCommand";
import { request } from "../util/Request";

const xml2js = require('xml2js');

class FastenerInstantiatedCommand extends PluginCommand {
  constructor() {
    super();
  }

  //解析xmL文件
  parseXML(formData,cb) {
    const reader = new FileReader();
    reader.onload = (event) => {
      const xmlString = event.target.result;
      this.parseXMLToJSON(xmlString).then(res=>{
        cb(res);
      });
    }
    reader.readAsText(formData);
  }

  parseXMLToJSON(xmlString){
    return new Promise(resolve => {
      const parser = new xml2js.Parser();
      parser.parseString(xmlString,(err,result)=>{
        if(err){
          this.eventBusAlertMsg('解析xml失败','error');
        }else {
          // let tableData = this.flattenObject(result);
          // console.log(tableData);
          resolve(result);
        }
      })
    })
  }
  flattenObject(obj,parentKey = '',result = []){
    for(let key in obj){
      if (obj.hasOwnProperty(key)){
        let newKey = parentKey ?`${parentKey}.${key}`:key;
        if (typeof  obj[key] === 'object' && obj[key] !== null){
          this.flattenObject(obj[key],newKey,result);
        }else {
          result.push({name:newKey.split('.').join(' '),value:obj[key]});
        }
      }
    }
    return result;
  }

  //直接在插件后端进行解析操作,
  parseXMLInJava(formData, cb) {
    let url = 'fastenerInstantiation/parseXMLFile'
    let data = formData;
    let config = {
      timeout: 5000,
    }
    return request.post(url, data, config).then(res => {
      if (res.status === 200 && res.data.code === 200) {
        cb(res.data);
      }
    }).catch(error => {
      console.log(error)
    })
  }

  //1.初始化要素库文件夹，获取元素属性
  //2.根据元素属性和模型id查询符合条件的模型列表
  //3.将模型列表中的模型插入

  initElementLibrary(cb) {
    let params = {
      cmdName: "getAllElementLibraryFolder",
    }
    this.executeElementLibraryCommand(params, res => {
      res.forEach(data => {
        if (!this.elementLibraryItemMap.has(data.id)) {
          let item = new LibraryItem(data);
          this.elementLibraryItemMap.set(item.id, item);
          if (data.folderCode === "0") {
            this.elementLibraryItems.push(item);
          } else {
            let parentItem = this.elementLibraryItemMap.get(data.folderCode);
            item.parent = parentItem;
            parentItem.children.push(item);
          }
        }
      })
      cb(this.elementLibraryItems);
    })
  }

  /**
   * 获取要素库中所有的文件夹
   * @param cb
   */
  executeElementLibraryCommand(params, cb) {
    if(this.toolQuery){
      this.toolQuery.executeElementLibraryCommand(params, this.commandId).then(res => {
        cb(res.data.data);
      })
    }
  }
  /**
   * 通过要素库插入对应零件
   * @param cb
   */
  executeInsertLibPartCommand(params,cb){
    if(this.toolQuery){
      this.toolQuery.elementLibraryPartInsert(params).then(res => {
        cb(res.data.data);
      })
    }
  }
}

const fastenerInstantiatedCommand = new FastenerInstantiatedCommand();
export { fastenerInstantiatedCommand };